﻿using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;

namespace AnyOffice.general.system.addressForPj
{
    public partial class address_edit : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int addressId = (Request.QueryString["address_id"] == null) ? 0 : int.Parse(Request.QueryString["address_id"].ToString());
                this.ViewState["AddressId"] = addressId;
                DataTable table = new AddressPjDao().SelectByAddressId(base.Conn, addressId, "");
                if (table.Rows.Count == 0)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此联系人");
                    return;
                }
                this.ViewState["GroupId"] = Convert.ToInt32(table.Rows[0]["GroupId"].ToString());
                this.txtName.Value = (table.Rows[0]["Name"] != DBNull.Value) ? table.Rows[0]["Name"].ToString() : "";
                if (this.ddlSex.Items.FindByValue(table.Rows[0]["Sex"].ToString()) != null)
                {
                    this.ddlSex.Items.FindByValue(table.Rows[0]["Sex"].ToString()).Selected = true;
                }
                this.txtBirthday.Value = (table.Rows[0]["Birthday"] != DBNull.Value) ? table.Rows[0]["Birthday"].ToString() : "";
                this.txtNickName.Value = (table.Rows[0]["NickName"] != DBNull.Value) ? table.Rows[0]["NickName"].ToString() : "";
                this.txtMinistration.Value = (table.Rows[0]["Ministration"] != DBNull.Value) ? table.Rows[0]["Ministration"].ToString() : "";
                this.txtMate.Value = (table.Rows[0]["Mate"] != DBNull.Value) ? table.Rows[0]["Mate"].ToString() : "";
                this.txtChild.Value = (table.Rows[0]["Child"] != DBNull.Value) ? table.Rows[0]["Child"].ToString() : "";
                this.txtDeptName.Value = (table.Rows[0]["DeptName"] != DBNull.Value) ? table.Rows[0]["DeptName"].ToString() : "";
                this.txtDeptAddr.Value = (table.Rows[0]["DeptAddr"] != DBNull.Value) ? table.Rows[0]["DeptAddr"].ToString() : "";
                this.txtDeptPost.Value = (table.Rows[0]["DeptPost"] != DBNull.Value) ? table.Rows[0]["DeptPost"].ToString() : "";
                this.txtDeptTel.Value = (table.Rows[0]["DeptTel"] != DBNull.Value) ? table.Rows[0]["DeptTel"].ToString() : "";
                this.txtDeptFax.Value = (table.Rows[0]["DeptFax"] != DBNull.Value) ? table.Rows[0]["DeptFax"].ToString() : "";
                this.txtHomeAddr.Value = (table.Rows[0]["HomeAddr"] != DBNull.Value) ? table.Rows[0]["HomeAddr"].ToString() : "";
                this.txtHomePost.Value = (table.Rows[0]["HomePost"] != DBNull.Value) ? table.Rows[0]["HomePost"].ToString() : "";
                this.txtHomeTel.Value = (table.Rows[0]["HomeTel"] != DBNull.Value) ? table.Rows[0]["HomeTel"].ToString() : "";
                this.txtMobile.Value = (table.Rows[0]["Mobile"] != DBNull.Value) ? table.Rows[0]["Mobile"].ToString() : "";
                this.txtBp.Value = (table.Rows[0]["Bp"] != DBNull.Value) ? table.Rows[0]["Bp"].ToString() : "";
                this.txtEmail.Value = (table.Rows[0]["Email"] != DBNull.Value) ? table.Rows[0]["Email"].ToString() : "";
                this.txtOicq.Value = (table.Rows[0]["Oicq"] != DBNull.Value) ? table.Rows[0]["Oicq"].ToString() : "";
                this.txtMsn.Value = (table.Rows[0]["Msn"] != DBNull.Value) ? table.Rows[0]["Msn"].ToString() : "";
                this.txtNote.Value = (table.Rows[0]["Note"] != DBNull.Value) ? table.Rows[0]["Note"].ToString() : "";
                ListItem item = new ListItem("默认", "0");
                if ("0" == table.Rows[0]["GroupId"].ToString())
                {
                    item.Selected = true;
                }
                this.ddlGroupId.Items.Add(item);
                DataTable table2 = new AddressGroupPjDao().SelectAll(base.Conn, "");
                if (table2.Rows.Count > 0)
                {
                    for (int i = 0; i < table2.Rows.Count; i++)
                    {
                        item = new ListItem(table2.Rows[i]["GroupName"].ToString(), table2.Rows[i]["GroupId"].ToString());
                        if (table2.Rows[i]["GroupId"].ToString() == table.Rows[0]["GroupId"].ToString())
                        {
                            item.Selected = true;
                        }
                        this.ddlGroupId.Items.Add(item);
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                AddressPjDao dao3 = new AddressPjDao();
                AddressPjData data = new AddressPjData();
                data.AddressId = Convert.ToInt32(this.ViewState["AddressId"].ToString());
                data.GroupId = Convert.ToInt32(this.ddlGroupId.SelectedValue);
                data.Name = this.txtName.Value;
                data.Sex = Convert.ToBoolean(this.ddlSex.SelectedValue);
                data.Birthday = this.txtBirthday.Value;
                data.NickName = this.txtNickName.Value;
                data.Ministration = this.txtMinistration.Value;
                data.Mate = this.txtMate.Value;
                data.Child = this.txtChild.Value;
                data.DeptName = this.txtDeptName.Value;
                data.DeptAddr = this.txtDeptAddr.Value;
                data.DeptPost = this.txtDeptPost.Value;
                data.DeptTel = this.txtDeptTel.Value;
                data.DeptFax = this.txtDeptFax.Value;
                data.HomeAddr = this.txtHomeAddr.Value;
                data.HomePost = this.txtHomePost.Value;
                data.HomeTel = this.txtHomeTel.Value;
                data.Mobile = this.txtMobile.Value;
                data.Bp = this.txtBp.Value;
                data.Email = this.txtEmail.Value;
                data.Oicq = this.txtOicq.Value;
                data.Msn = this.txtMsn.Value;
                data.Note = this.txtNote.Value;
                dao3.Update(base.Conn, data);
                StringBuilder builder = new StringBuilder("<script>\n");
                builder.Append("parent.menu.location.reload();\n");
                builder.Append("parent.main.location='address.aspx?group_id=" + this.ddlGroupId.SelectedValue + "';\n");
                builder.Append("</script>\n");
                if (!this.Page.IsClientScriptBlockRegistered("AddressGroup_list"))
                {
                    this.Page.RegisterClientScriptBlock("AddressGroup_list", builder.ToString());
                }
            }
        } 

    }
}
